import {
  getGoodsCateListReq,
  deleteGoodsCateReq,
  addOrEditGoodsCateReq,
} from '../request/goods-cate'
const actions = {
  // 获取所有商品分类
  async getGoodsCateList(context, params) {
    try {
      const goodsCateListData = await getGoodsCateListReq(params)
      // 判断是获取一二级分类，还是分页获取全部分类
      if (params.type !== 2) {
        context.commit('GET_GOODS_CATE_LIST', goodsCateListData)
      } else {
        return goodsCateListData
      }
    } catch (error) {
      console.log(error)
    }
  },
  // 删除商品分类
  async deleteGoodsCate(context, id) {
    try {
      const deleteGoodsCateData = await deleteGoodsCateReq(id)
      return deleteGoodsCateData
    } catch (error) {
      return Promise.reject(error)
    }
  },
  // 添加或编辑分类
  async addOrEditGoodsCate(context, params) {
    try {
      const addOrEditGoodsCateData = await addOrEditGoodsCateReq(
        params.id,
        params
      )
      return addOrEditGoodsCateData
    } catch (error) {
      return Promise.reject(error)
    }
  },
}
const mutations = {
  GET_GOODS_CATE_LIST(state, goodsCateListData) {
    state.goodsCateListData = goodsCateListData
  },
}
const state = {
  goodsCateListData: {}, // 商品分类列表对象
}
const getters = {
  // 商品分类列表
  goodsCateList(state) {
    return state.goodsCateListData.result
  },
}

export default {
  namespaced: true,
  actions,
  mutations,
  state,
  getters,
}
